<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }" style="margin-left: 20px">爱心关怀管理</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">爱心关怀</a></el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入姓名..." suffix-icon="el-icon-search" class="ml-5"
                v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="seachKehu">搜索</el-button>
      <el-button @click="resetForm">重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="primary" @click="openKehudialog">新增<i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
      <el-button type="danger" @click="delbath">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
      <el-upload
          action="http://localhost:8888/api/helpold/import" style="display: inline-block" :show-file-list="false"
          :accept="xls" :on-success="impOK" :on-error="imperr"
      >
        <el-button type="primary" class="ml-5">导入<i class="el-icon-bottom" style="margin-left: 2px"></i></el-button>
      </el-upload>
      <el-button type="primary" @click="exp" class="ml-5">导出<i class="el-icon-top" style="margin-left: 2px"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="50" fixed></el-table-column>
      <el-table-column prop="name" label="姓名" width="100px">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="70px">
      </el-table-column>
      <el-table-column prop="physical" label="身体状况" width="100px">
        <template v-slot="{ row }">
          <span v-if="row.physical === '1'">健康</span>
          <span v-else-if="row.physical === '2'">偶尔生病</span>
          <span v-else-if="row.physical === '3'">经常生病</span>
          <span v-else>身体不适</span>
        </template>
      </el-table-column>
      <el-table-column prop="pension" label="养老金发放" width="100px">
        <template v-slot="{ row }">
          <span v-if="row.pension === '0'">已发放</span>
          <span v-else-if="row.pension === '1'">未发放</span>
        </template>
      </el-table-column>
      <el-table-column prop="note" label="备注" width="160px">
      </el-table-column>
      <el-table-column prop="audit" label="审核状态" width="80px">
        <template v-slot="{ row }">
          <span v-if="row.audit === '1'">未审核</span>
          <span v-else-if="row.audit === '2'">通过</span>
          <span v-else-if="row.audit === '3'">拒绝</span>
        </template>
      </el-table-column>
      <el-table-column prop="audit" label="审核" width="110px">
        <template v-slot="{ row }">
          <span v-if="row.audit == '1'"><el-button type="danger" plain  @click="auditOk(row)">待审核</el-button></span>
          <span v-else><el-button type="success" plain @click="dialog(row)">已审核</el-button></span>
        </template>

      </el-table-column>
      <el-table-column prop="relf" label="拒绝原因" width="150px">
      </el-table-column>
      <el-table-column label="操作"  fixed="right" width="200px">
        <template slot-scope="scope">
          <el-button type="primary"  @click="updatekehu(scope.row)"><i class="el-icon-edit"></i>编辑</el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定要删除这条记录吗？"
              @confirm="deletekehu(scope.row.id)"
          >
            <el-button type="danger" slot="reference"><i class="el-icon-remove-outline"></i>删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10,15,20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>


    <el-dialog title="爱心关怀" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-position="left">
        <el-form-item label="姓名" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.name" autocomplete="off" placeholder="请输入姓名" style="width: 220px"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="年龄" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.age" autocomplete="off" placeholder="请输入年龄" style="width: 220px" type="number"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="身体状况" label-width="100px">
          <el-col :span="16">
            <el-select v-model="form.physical" placeholder="请选择" style="width: 220px">
              <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="备注" label-width="100px">
          <el-col :span="16">
            <el-input v-model="form.note" autocomplete="off" placeholder="请输入内容" style="width: 220px" type="textarea"
                      :autosize="{ minRows: 2, maxRows: 4}"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="养老金发放" label-width="100px">
          <el-col :span="16">
            <el-radio-group v-model="form.pension" style="width: 250px">
              <el-radio :label="'0'">已发放</el-radio>
              <el-radio :label="'1'">未发放</el-radio>
            </el-radio-group>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveKehu">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核信息" :visible.sync="dialogFormVisible2">
      <el-form :model="form">
        <el-form-item label="审核">
          <el-select v-model="form.audit" placeholder="请选择">
            <el-option label="通过" value="2"></el-option>
            <el-option label="拒绝" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="拒绝原因">
          <el-input v-model="form.relf"type="textarea"
                    :autosize="{ minRows: 2, maxRows: 4}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="open()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>


<script>

import request from "@/utils/request";
export default {
  name: "Helpold",
  data(){
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: "",
      form:{
        id:"",
        name:"",
        age:"",
        physical:"",
        pension:"",
        note:"",
        audit:"",
        relf:""
      },
      dialogFormVisible: false,
      dialogFormVisible2: false,
      headerBg: 'headerBg',
      multipleSelection: [],
      options: [{
        value: '1',
        label: '健康'
      }, {
        value: '2',
        label: '偶尔生病'
      }, {
        value: '3',
        label: '经常生病'
      }, {
        value: '4',
        label: '身体不适'
      }],
    }
  },
  created() {
    this.load()
  },
  methods: {
    auditOk(data){
      this.form={}
      this.dialogFormVisible2=true;
      this.form.id=data.id

    },
    dialog(row){
      this.dialogFormVisible2=true;
      this.form=row;
    },
    open() {
      this.dialogFormVisible2=false
      request.post("helpold/update",this.form).then(res =>{
        if (res.code=="200"){
          this.load()
          this.$message.success("审核成功")
        }else {
          this.$message.error("审核失败")
        }
        this.form={}
      })
    },
    imperr(){
      this.$message.error("文件导入失败")
    },
    impOK(){
      this.$message.success("文件导入成功")
      this.load()
    },
    exp(){
      window.open("http://localhost:8888/api/helpold/export");
    },
    delbath(){
      let ids=this.multipleSelection.map(v =>v.id)
      request.post("helpold/deletebatch",ids).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    deletekehu(id) {
      console.log(id)
      request.post("helpold/delete",id).then(res => {
        if (res.code == "200") {
          this.load()
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    updatekehu(row){
      this.form=row
      this.dialogFormVisible=true
    },
    saveKehu(){
      this.dialogFormVisible = false
      if (this.form.id){
        request.post("helpold/update",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("修改信息成功")
          }else {
            this.$message.error("修改信息失败")
          }
        })
      }else {
        this.form.audit=1
        request.post("helpold",this.form).then(res =>{
          if (res.code=="200"){
            this.load()
            this.$message.success("新增成功")
          }else {
            this.$message.error("新增失败")
          }
        })
      }

    },
    openKehudialog(){
      this.form={}
      this.dialogFormVisible=true
    },
    resetForm() {
      this.name = ""
      this.load()
    },
    seachKehu() {
      this.load()
    },
    handleCurrentChange(data) {
      this.pageNum = data
      this.load()
    },
    handleSizeChange(data) {
      this.pageSize = data
      this.load()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    load() {
      request.get("helpold/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name
        }
      }).then(res => {
        if (res.code=="200"){
          this.tableData = res.data.records
          this.total = res.data.total
        }

      })
    }
  }

}
</script>
<style scoped>

</style>